package com.open.capacity.sign.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Sign;
import com.open.capacity.common.model.SignReport;
import com.open.capacity.common.model.SysParam;
import com.open.capacity.sign.service.SignService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.common.web.Result;
import com.open.capacity.common.web.PageResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sign")
@Api(tags = "SIGN API")
public class SignController {
    @Autowired
    private SignService signService;

    /**
     *用户签退
     * @param sign
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signOut")
    @ApiOperation(value = "用户签退")
    @LogAnnotation(module = "signIn-center")
    public Result signOut(@RequestBody Sign sign) throws ControllerException{
        try {
            return signService.signOut(sign);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 用户签到
     * @param sign
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signIn")
    @ApiOperation(value = "用户签到")
    @LogAnnotation(module = "signIn-center")
    public Result signIn(@RequestBody Sign sign) throws ControllerException{
        try {
            return signService.signIn(sign);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 查询当前登录用户的所有签到记录
     * @param params
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signManage")
    @ApiOperation(value = "签到管理")
    @LogAnnotation(module = "signIn-center")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    public PageResult<Sign> signManage(@RequestParam Map<String, Object> params) throws ControllerException{
        try {
            return signService.signManage(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 导出签到数据
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/exportSign")
//    @PreAuthorize("hasAuthority('user:post/users/exportDaily')")
    public void exportSign(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {
            List<Sign> result = signService.findAllSigns(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("签到导出", "签到"),
                    Sign.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        } catch (IOException e) {
            throw new ControllerException(e);
        }
    }
    /**
     * 导出考勤数据
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/exportKaoqin")
//    @PreAuthorize("hasAuthority('user:post/users/exportDaily')")
    public void exportKaoqin(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) throws ControllerException {
        try {
            List<SignReport> result = signService.findAllKaoqin(params);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");
            @Cleanup OutputStream ouputStream = null;
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("考勤导出", "考勤"),
                    SignReport.class, result);
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        } catch (IOException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 将签到签退记录修改，修改时间；修改状态
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signUpdate")
    @ApiOperation(value = "记录修改")
    @LogAnnotation(module = "signIn-center")
    public Result signUpdate(@RequestBody Sign sign) throws ControllerException{
        try {
            return signService.signUpdate(sign);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 将签到签退记录删除
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signDelete")
    @ApiOperation(value = "记录修改")
    @LogAnnotation(module = "signIn-center")
    public Result signDelete(@RequestBody Sign sign) throws ControllerException{
        try {
            return signService.signDelete(sign);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取参数
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/getParamByAttr")
    @ApiOperation(value = "参数修改")
    @LogAnnotation(module = "signIn-center")
    public List<SysParam> getParamByAttr(@RequestBody SysParam sysParam) throws ControllerException{
        try {
            return signService.getParamByAttr(sysParam);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 将签到参数修改
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signParamUpdate")
    @ApiOperation(value = "记录修改")
    @LogAnnotation(module = "signIn-center")
    public Result signParamUpdate(@RequestBody List<SysParam> list) throws ControllerException{
        try {
            return signService.signParamUpdate(list);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 将签到计数
     *
     * @return
     * @throws ControllerException
     */
    @PostMapping("/signCount")
    @ApiOperation(value = "签到计数")
    @LogAnnotation(module = "signIn-center")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    public PageResult<SignReport> signCount(@RequestParam Map<String, Object> params) throws ControllerException{
        try {
            return signService.signCount(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }
}
